<script setup>
import { ref, reactive } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
// 假设您有注册相关的 API 服务
import { registerServiceAPI } from '@/api/user';
const router = useRouter();
const registerFormRef = ref();
const registerForm = reactive({
  userId: '',
  password: '',
  confirmPassword: '',
  role: '0',
  name: '',
  gender: '',
  email: '',
  phone: '',
  classInfo: '',
  department: ''
});

const validateConfirmPassword = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次输入密码'));
  } else if (value !== registerForm.password) {
    callback(new Error('两次输入的密码不一致'));
  } else {
    callback();
  }
};

const rules = reactive({
  userId: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 6, max: 9, message: '长度在 6 到 9 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    { validator: validateConfirmPassword, trigger: 'blur' }
  ],
  name: [
    { required: true, message: '请输入姓名', trigger: 'blur' },
    { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
  ],
  gender: [
    { required: true, message: '请选择性别', trigger: 'change' }
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  role: [
    { required: true, message: '请选择角色', trigger: 'change' }
  ],
  classInfo: [
    {
      required: true,
      message: '请输入班级',
      trigger: 'blur',
      // 只有当 role 为 '0' 时才校验
      validator: (rule, value, callback) => {
        if (registerForm.role === '0' && !value) {
          callback(new Error('请输入班级'));
        } else {
          callback();
        }
      }
    }
  ],
  department: [
    {
      required: true,
      message: '请输入部门',
      trigger: 'blur',
      // 只有当 role 为 '1' 时才校验
      validator: (rule, value, callback) => {
        if (registerForm.role === '1' && !value) {
          callback(new Error('请输入部门'));
        } else {
          callback();
        }
      }
    }
  ]
});
// 切换到登录页
const switchToLogin = () => {
  // 假设您有一个路由到登录页
  router.push('/login');
};
const handleRegister = () => {
  registerFormRef.value.validate(async (valid) => {
    if (valid) {
      console.log('注册表单提交', registerForm);
      // 在这里调用您的注册 API 服务
      try {
        const res = await registerServiceAPI(registerForm);
        console.log(res);
        if (res.data.code === 200) {
          ElMessage.success('注册成功');
          router.push('/login');
        }else if (res.data.code === 500) {
          ElMessage.error(res.data.msg);
        }

      } catch (error) {
        ElMessage.error('注册失败');
        console.error('注册失败:', error);
      }
    } else {
      console.log('表单校验失败');
      return false;
    }
  });
};
</script>

<template>
  <div class="register-container">
    <el-card class="register-card">
      <h2>注册</h2>
      <el-form :model="registerForm" :rules="rules" ref="registerFormRef">
        <el-form-item prop="role">
          <el-select v-model="registerForm.role" placeholder="请选择角色" style="width: 100%;">
            <el-option label="学生" value="0"></el-option>
            <el-option label="老师" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="userId">
          <el-input v-model="registerForm.userId" placeholder="用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="registerForm.password" type="password" placeholder="密码"></el-input>
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <el-input v-model="registerForm.confirmPassword" type="password" placeholder="确认密码"></el-input>
        </el-form-item>
        <el-form-item prop="name">
          <el-input v-model="registerForm.name" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item prop="gender">
          <el-select v-model="registerForm.gender" placeholder="请选择性别" style="width: 100%;">
            <el-option label="男" value="男"></el-option>
            <el-option label="女" value="女"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="email">
          <el-input v-model="registerForm.email" placeholder="邮箱"></el-input>
        </el-form-item>
        <el-form-item prop="phone">
          <el-input v-model="registerForm.phone" placeholder="手机号"></el-input>
        </el-form-item>
        <el-form-item prop="classInfo" v-if="registerForm.role === '0'">
          <el-input v-model="registerForm.classInfo" placeholder="班级"></el-input>
        </el-form-item>
        <el-form-item prop="department" v-if="registerForm.role === '1'">
          <el-input v-model="registerForm.department" placeholder="部门"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleRegister" style="width: 100%">注册</el-button>
        </el-form-item>
        <el-form-item>
          <el-link type="primary" @click="switchToLogin" underline="never">已有账号？去登录</el-link>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f5f5;
}

.register-card {
  width: 400px;
  padding: 20px;
}

h2 {
  text-align: center;
  margin-bottom: 20px;
}
</style>